// pages/news/newsDetails/newsDetails.js
const app = getApp();
//解析html
const WxParse = require('../../../wxParse/wxParse.js');
import Api from '../../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fabulous: false,//点赞状态
    like: false,//喜欢状态
    newsList: [],
    newsDetails:'',
    lid:null,//当前新闻id
  },
  //点赞
  bindFabulous: function(e){
    console.log(this.data.newsDetails.is_click);
    if (this.data.newsDetails.is_click == 0) {
      console.log(1);
      this.getClickStatus(e);
    } else {
      console.log(2);
      this.getClick(e);
    }
    
  },
 
  //跳转新闻详情
  toNewsDetails: function(e){
    var that=this;
    console.log(e)
    var  {newsList} = that.data
    let index = e.currentTarget.dataset.index
    var lid = newsList[index].id
     let type=that.data.options.type
    wx.navigateTo({
      url: `/pages/news/newsDetails/newsDetails?lid=${lid}&type=${type}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    that.setData({
      options: options,
      lid:options.lid
    });
    that.getNewsList();
    that.getBrowse();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getNewDetails();
  },


  //获取新闻列表
  getNewsList:function(){
    var that=this;
    var param = {
      type: parseInt(that.data.options.type),
      pageNum: 1,
      pageSize: 3,
    }
    console.log(param)
    Api.ajax("/app/Zarticle/articleList", param).then(res => {
      console.log(res)
      if(res.code==200){
        that.setData({
          newsList: res.data
        })
      }
    })
  },
  //新闻详情
  getNewDetails:function(){
    var that = this;
    console.log(that.data)
    var uid = wx.getStorageSync('userid');
      var param = {
        uid: uid.id,
        aid: parseInt(that.data.options.lid),
      }
    console.log(param)
    Api.ajax("/app/Zarticle/articleDetails", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        var article=res.data.content;
        WxParse.wxParse('article', 'html', article, that);
        that.setData({
          newsDetails: res.data
        })
      }
    })
  },
  //浏览
  getBrowse: function (e) {
    var that = this;
    let uid = wx.getStorageSync('userid')
    var param = {
      aid: that.data.options.lid,
      uid: uid.id,
      type: 2,
    }
    console.log(param)
    Api.ajax("/app/Zarticle/giveLook", param).then(res => {
      console.log(res)
      if (res.code == 200) {
      } 
    })
  },
  //点赞
  getClickStatus: function (e) {
    var that = this;
    let uid=wx.getStorageSync('userid')
    var param = {
      aid: parseInt(that.data.options.lid),
      uid:uid.id,
      type: 1
    }
    console.log(param)
    Api.ajax("/app/Zarticle/giveLook", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '点赞成功',
          icon: 'none',
          duration: 1000,
        });
        setTimeout(function() {
          that.getNewDetails();
       }, 2000);
      }else {
        wx.showToast({
          title: '已点赞',
          icon: 'none',
          duration: 1000,
        })
      }
    })
  },
  //取消点赞 
  getClick: function (e) {
    var that = this;
    let uid = wx.getStorageSync('userid')
    var param = {
      aid: parseInt(that.data.options.lid),
      uid: uid.id,
    }
    console.log(param)
    Api.ajax("/app/Zarticle/deleteClick", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '取消成功',
          icon: 'none',
          duration: 1000,
        })
        setTimeout(function () {
          that.getNewDetails();
        }, 2000);
      } else {
        wx.showToast({
          title: '已点赞',
          icon: 'none',
          duration: 1000,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})